<?php
namespace Home\Controller;
use Think\Controller;
class VideoController extends CommonController {

    //显示视频列表
    public function videolist(){

        $video=M('video');
        //$this->coachlist=$coach->join('club on coach.coach_club_id=club.club_id')->select();
        $this->videolist=$video->join('coach on video.coach_id=coach.coach_id')->select();
        //显示编号
        $cnt=1;
        $this->assign('cnt',$cnt);
        //dump($this->videolist);
        $this->display();

    }

    public function delvideo(){

        $id=I('video_id');
        $video=M('video');
        $res=$video->where("video_id=$id")->find();
        // echo $res;

        if($video->delete($id))
        {
            $this->success('删除成功！');
        }else
        {
            $this->error('删除失败！');
        }
    }

    //显示添加视频信息界面
    public function videoAdd()
    {
        $this->display();
    }

    //添加视频
    public function dovideoadd()
    {
        $video = D("video");

        if (!$video->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            exit($video->getError());
        } else {
            // 创建成功，做其他事情
            if ($video->create()) {
                $pname = 'default.mp4';
                if ($picinfo = $this->upload()) {
                    $pname = $picinfo['file']['savename'];
                }
            }
            $video->video_addr = $pname;
            if ($video->add()) {
                $this->success('添加成功' ,'./videolist', 3);
            } else {
                $this->error("添加失败");
            }
        }


    }


    //显示视频详情
    public function videomod($video_id)
    {

        $video = M('video');

        $this->videoInfo = $video->select($video_id);
        $this->display();
    }

    //修改视频
    public function dovideomod($video_id)
    {

        $Video = D('video');

        $videoInfo = $Video->select($video_id);

        if (!$Video->create()) {
//             如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Video->getError());
        } else {
            // 创建成功，做其他事情
            if ($Video->create()) {
                //判断是否上传了新的图片
                if ($_FILES['file']['error'] == 0) { //如果有文件上传
                    //删除之前的文件
                    unlink('./Public/upload/videos/' . $videoInfo['0']['video_addr']);
                    //上传新的文件
                    $pname = 'default.jpg';
                    if ($picinfo = $this->upload()) {
                        $pname = $picinfo['file']['savename'];
                    }
                    $Video->video_addr = $pname;
                }
                $Video->video_id = $video_id;
                if ($Video->save() !== false) {
                    $this->success('修改成功', './videoList', 3);
                } else {
                    $this->error("修改失败");
                }
            }
        }
    }


<<<<<<< HEAD
=======
    //显示视频详情
    public function videomod($video_id)
    {

        $video = M('video');

        $this->videoInfo = $video->select($video_id);
        $this->display();
    }

    //修改视频
    public function dovideomod($video_id)
    {

        $Video = D('video');

        $videoInfo = $Video->select($video_id);

        if (!$Video->create()) {
//             如果创建失败 表示验证没有通过 输出错误提示信息
            exit($Video->getError());
        } else {
            // 创建成功，做其他事情
            if ($Video->create()) {
                //判断是否上传了新的图片
                if ($_FILES['file']['error'] == 0) { //如果有文件上传
                    //删除之前的文件
                    unlink('./Public/upload/videos/' . $videoInfo['0']['video_addr']);
                    //上传新的文件
                    $pname = 'default.jpg';
                    if ($picinfo = $this->upload()) {
                        $pname = $picinfo['file']['savename'];
                    }
                    $Video->video_addr = $pname;
                }
                $Video->video_id = $video_id;
                if ($Video->save() !== false) {
                    $this->success('修改成功', './videoList', 3);
                } else {
                    $this->error("修改失败");
                }
            }
        }
    }


>>>>>>> 62e237362e20a94a945bc25068926c792b5c027b


    //文件上传upload函数
    public function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize =  3145728000 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg',"mp4","wmv");// 设置附件上传类型
        $upload->rootPath = './Public/upload/videos/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub = false;
        $upload->saveName = time() . mt_rand(); //实际保存文件名：时间戳+随机数
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
//            $this->success('上传成功！');
            return $info;
        }

    }



    public function interfaceVideo()
    {
        $id = I('get.id');

        $video = M('video');
        $result = $video->select();
        if($result!=false)
        {
            $data = array('results' => $result);
            $this->ajaxReturn($data,'JSON');
        }
        else{
            echo "获取失败!";
        }
    }

    public function interfaceVideoInfo()
    {
        $id=I('get.video_id');
        $videoInfo=M('video');
        $result = $videoInfo->where("video_id=$id")->find();
        if($result!=false)
        {
            $data = array('results' => $result);
            $this->ajaxReturn($data,'JSON');
        }
        else{
            echo "获取失败!";
        }
    }




}